package com.dreamer.repository.mobile;

import com.dreamer.domain.pmall.goods.PmallGoods;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.*;
import org.springframework.stereotype.Repository;
import ps.mx.otter.utils.SearchParameter;

import java.util.List;

/**
 * Created by huangfei on 12/07/2017.
 */
@Repository
public class MallGoodsDao extends BaseDaoImpl<PmallGoods> {

    private Session getCurrentSession() {
        return getSessionFactory().getCurrentSession();
    }

    public List<PmallGoods> findMallGoods(SearchParameter<PmallGoods> p) {
//        p.getEntity().setSubGoods("优惠商城");
        DetachedCriteria dc = DetachedCriteria.forClass(PmallGoods.class);
        Example example = Example.create(p.getEntity()).enableLike(MatchMode.ANYWHERE);
        if(p.getEntity().getGoodsType()!=null&&p.getEntity().getGoodsType().getId()!=null){
            dc.add(Restrictions.eq("goodsType.id",p.getEntity().getGoodsType().getId()));
        }
        dc.add(example);
        dc.addOrder(Order.desc("sequence"));
        return searchByPage(p,dc);
    }

//    //TODO 置换商城
//    public List<PmallGoods> findMallGoods_sub(SearchParameter<PmallGoods> p) {
//        p.getEntity().setSubGoods("置换商城");
//        DetachedCriteria dc = DetachedCriteria.forClass(PmallGoods.class);
//        Example example = Example.create(p.getEntity()).enableLike(MatchMode.ANYWHERE);
//        if(p.getEntity().getGoodsType()!=null&&p.getEntity().getGoodsType().getId()!=null){
//            dc.add(Restrictions.eq("goodsType.id",p.getEntity().getGoodsType().getId()));
//        }
//        dc.add(example);
//        dc.addOrder(Order.desc("sequence"));
//        return searchByPage(p,dc);
//    }

    public List<PmallGoods> findMallGoods_zh(SearchParameter<PmallGoods> p) {
        DetachedCriteria dc = DetachedCriteria.forClass(PmallGoods.class,"p");
        Example example = Example.create(p.getEntity()).enableLike(MatchMode.ANYWHERE);
        if(p.getEntity().getGoodsType()!=null&&p.getEntity().getGoodsType().getId()!=null){
            dc.add(Restrictions.eq("goodsType.id",p.getEntity().getGoodsType().getId()));
        }
        dc.add(example);
        dc.add(Restrictions.eq("p.canAdvance",true));
        dc.addOrder(Order.asc("p.sort"));
        return searchByPage(p,dc);
    }

    public List<PmallGoods> findMallGoodss_zh() {
        String hql = "from PmallGoods as l where l.canAdvance=1  ORDER BY l.sort ASC";
        Query query = getCurrentSession().createQuery(hql);
        return query.list();
    }

}


